package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
@Autowired
private ClazzService clazzMapper;
    @Override
    public PageResult page(StudentQueryParam student) {

        IPage<Student> iPage =new Page<>(student.getPage(),student.getPageSize());
        LambdaQueryWrapper<Student> studentWrapper =new LambdaQueryWrapper<>();
        studentWrapper.like(student.getName()!=null&& !student.getName().isEmpty(),Student::getName,student.getName())
                        .eq(student.getDegree()!=null,Student::getDegree,student.getDegree())
                        .eq(student.getClazzId()!=null,Student::getClazzId,student.getClazzId());
        IPage<Student> page = page(iPage, studentWrapper);


        List<Student> records = page.getRecords();
        records.forEach(student1 -> {
            Clazz byId = clazzMapper.getById(student1.getClazzId());
            if (byId != null) {
                student1.setClazzName(byId.getName());
            }
        });
        return new PageResult(iPage.getTotal(), records);
    }
}
